package com.aaa.controller;


import com.aaa.dto.CarDto;
import com.aaa.dto.GoodsCountDto;
import com.aaa.pojo.DefaultMsg;
import com.aaa.pojo.Goods;
import com.aaa.pojo.Goodstype;
import com.aaa.service.CarService;
import com.aaa.service.GoodsService;
import com.aaa.service.GoodsTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 用户管理控制器
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {


    @Autowired
    private GoodsService goodsService;


    @Autowired
    private GoodsTypeService goodsTypeService;


    /**
     * 查询所有的用户信息
     *
     * @return
     */
    @RequestMapping("/listAll")
    public PageInfo<Goods> listAll(String keyword, Integer currentPage, Integer pageSize) {
        //开始分页
        PageHelper.startPage(currentPage, pageSize);
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<Goods>();
        queryWrapper.like("name", keyword);
        List<Goods> list = goodsService.list(queryWrapper);
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    /**
     * 保存或者修改购买信息
     *
     * @param goods
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    public DefaultMsg saveOrUpdate(Goods goods) {
        DefaultMsg defaultMsg = new DefaultMsg();
        boolean b = goodsService.saveOrUpdate(goods);
        if (!b) {
            defaultMsg.setMsg(0);
            defaultMsg.setTarget("操作失败");
        }
        return defaultMsg;
    }

    @RequestMapping("/listAllType")
    public List<Goodstype> listAllType() {
        List<Goodstype> i = goodsTypeService.list();
        return i;
    }

    @RequestMapping("/listAllState")
    public List<Goods> listAllState(String keyword, String name) {
        List<Goods> i = goodsService.listAllState(keyword, name);
        System.out.println(i);
        return i;
    }

    @RequestMapping("/gwc")
    public Goods listAllById(Integer id) {
        return goodsService.listAllById(id);
    }

    @RequestMapping("/findGoodsCountByGoodsId")
    public List<GoodsCountDto> findGoodsCountByGoodsId(@RequestBody Map<String,List<Integer>>  goodsIdList) {
        return goodsService.findGoodsCountByGoodsId(goodsIdList);
    }

}
